<?php 
namespace app\tune\controller;
use think\Controller;
use think\Db;

class Mistine extends controller {
  public function index() {
    $goods = Db::table('tp_goods')->where(['brand_id' => 369])->select();
    //var_dump($goods);
    foreach ($goods as $key => $good) {
      // $this->updateGoodsSN($good['goods_id'], str_pad(($key + 1), 3, '0', STR_PAD_LEFT));
//       $image_id = intval(substr($good['goods_sn'], strpos($good['goods_sn'], 'e') + 1)) * 2 - 1;
//       $filename = 'image' . str_pad($image_id, 3, '0', STR_PAD_LEFT) . '.png';
//       if ($this->updateImagePath($good['goods_id'], $filename)) {
//         $this->removeThumb($good['goods_id']);
//       }
    }
  }
  
  // 更新货号
  public function updateGoodsSN($goods_id, $sn_id) {
    $prefix = 'mistine';
    Db::table('tp_goods')
      ->where('goods_id', $goods_id)
      ->update(['goods_sn' => $prefix . $sn_id]);
    var_dump(Db::table('tp_goods')->where('goods_id', $goods_id)->value('goods_sn'));
  }
  
  // 删除缩微图目录
  private function removeThumb($goods_id) {
    $thumb_base = '/var/www/html/tpshop/public/upload/goods/thumb';
    if (isset($goods_id)) {
      $thumb_path = $thumb_base . DIRECTORY_SEPARATOR . $goods_id;
      var_dump(file_exists($thumb_path));
      if (file_exists($thumb_path)) {
        array_map('unlink', glob($thumb_path."/*"));
        rmdir($thumb_path);
      }
    }
    
  }
  
  // 更新图片路径
  private function updateImagePath($goods_id, $filename) {
    $image_base = '/public/upload/goods/2017/07-22/';
    $original_image_path = $image_base . $filename;
    return Db::table('tp_goods')
      ->where('goods_id', $goods_id)
      ->setField('original_img', $original_image_path);
  }
}